// License Notification
//======================================================
// Copyright (c) 2010 Channel Intelligence Inc.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// ======================================================




using System;
using System.Windows.Forms;
using CIP4.Simple;

namespace CIP4.Browser
{
    public partial class EditConnectionForm : Form
    {
        ViewType View { get; set; }

        public EditConnectionForm(ViewType view)
        {
            InitializeComponent();
            View = view;
            foreach (Type subType in typeof(CIP4ConnectionAttribute).Assembly.GetTypes())
                if (subType.BaseType == typeof(CIP4ConnectionAttribute))
                    ConnectionTypeComboBox.Items.Add(new ConnectionTypeWrapper { TheType = subType });
            ConnectionDataPropertyGrid.SelectedObject = new ConnectionWrapper(View);
        }

        private void ConnectionTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!(ConnectionTypeComboBox.SelectedItem is ConnectionTypeWrapper))
                return;
            ConnectionDataPropertyGrid.SelectedObject = 
                new ConnectionWrapper(View, ((ConnectionTypeWrapper)ConnectionTypeComboBox.SelectedItem).TheType);
        }

        class ConnectionTypeWrapper
        {
            public Type TheType { get; set; }
            
            public override string ToString()
            {
                return TheType.Name;
            }
        }

        class ConnectionWrapper
        {
            ViewType View { get; set; }

            // These are used by the property grid - resharper can't see it apparently
            // ReSharper disable UnusedMember.Local
            // ReSharper disable MemberCanBePrivate.Local
            // ReSharper disable UnusedAutoPropertyAccessor.Local

            public string Collection
            {
                get { return View.CollectionName; }
                set { View.CollectionName = value; }
            }

            public CIP4ConnectionAttribute Connection { get; set; }
            
            // ReSharper restore UnusedAutoPropertyAccessor.Local
            // ReSharper restore UnusedMember.Local
            // ReSharper restore MemberCanBePrivate.Local

            public ConnectionWrapper(ViewType view, Type connectionType)
            {
                View = view;
                Collection = view.CollectionName;
                View.DataConnection = (CIP4ConnectionAttribute)Activator.CreateInstance(connectionType);
                Connection = View.DataConnection;
            }

            public ConnectionWrapper(ViewType view)
            {
                View = view;
                Connection = view.DataConnection;
            }
        }
    }

}
